import api from '../../utils/api'
import { numberQuery, propertyQuery, numberCheckout } from '../../utils/conf'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dialog: false,
    checkout: false,
    houses: [],
    house_id: 0,
    house_name: '',
    leave_time: '',
    leave_id: 0,
    list: []
  },
  close: function () {
    this.setData({
      dialog: false,
      checkout: false
    })
  },
  openIOS1: function () {
    this.setData({
      dialog: true
    });
  },
  openCheckout: function (e) {
    let { index } = e.currentTarget.dataset
    this.setData({
      checkout: true,
      leave_id: this.data.list[index].id
    });
  },
  bindDateChange: function (e) {
    this.setData({
      leave_time: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryHouse()
    this.query()
  },

  queryHouse() {
    let that = this
    api.get(propertyQuery)
      .then(res => {
        // console.log(res)
        let data = res.data
        that.setData({
          houses: data,
          house_name: that.selectHouse(data, res.msg),
          house_id: res.msg
        })
      }).catch(err => {
        console.log(err)
      })
  },

  selectHouse(arrayList, house_id) {
    let house_name = ''
    for (let index = 0; index < arrayList.length; index++) {
      if (arrayList[index].id == house_id) {
        house_name = arrayList[index].name
        break
      }
    }
    return house_name
  },

  query(house_id = 0) {
    let that = this
    if (house_id) {
      api.post(numberQuery, {
        house_property_id: house_id
      })
        .then(res => {
          // console.log(res)
          that.setData({
            list: res.data,
            leave_time: res.msg
          })
        }).catch(err => {
          console.log(err)
        })
    } else {
      api.get(numberQuery)
        .then(res => {
          console.log(res)
          that.setData({
            list: res.data,
            leave_time: res.msg
          })
        }).catch(err => {
          console.log(err)
        })
    }
  },

  houseChange(e) {
    // console.log(e.target.dataset.id)
    this.setData({
      house_name: this.selectHouse(this.data.houses, e.target.dataset.id),
      meter_time: '',
      house_id: e.target.dataset.id
    })
    this.query(e.target.dataset.id)
    this.close()
  },

  onModifyTap(e) {
    let { index } = e.currentTarget.dataset
    // console.log(this.data.list[index].id)
    let id = this.data.list[index].id
    wx.navigateTo({
      url: '/pages/number/modify?id=' + id
    })
  },

  onRentalTap(e) {
    let { index } = e.currentTarget.dataset
    // console.log(this.data.list[index].id)
    let { id, house_property_id } = this.data.list[index]
    wx.navigateTo({
      url: '/pages/number/rental?id=' + id + '&house_property_id=' + house_property_id
    })
  },

  onCheckoutTap() {
    let that = this
    api.post(numberCheckout, {
      id: this.data.leave_id,
      leave_time: this.data.leave_time
    })
      .then(res => {
        this.setData({
          checkout: false
        })
        // console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 1000,
          success: function () {
            setTimeout(() => {
              that.query()
            }, 1000);
          }
        })
      }).catch(err => {
        this.setData({
          checkout: false
        })
        console.log(err)
        wx.showToast({
          title: err.msg,
          icon: 'error',
          duration: 1000
        })
      })
  },
})